<template>
  <div class="product-card">
    <router-link :to="`/product/${product.id}`" class="card-link">
      <!-- 商品图片 -->
      <div class="product-image">
        <div
          class="image-placeholder"
          :style="{ backgroundColor: product.image ? 'transparent' : '#f8f8f8' }"
        >
          <!-- 实际项目中这里显示商品图片 -->
          <div v-if="!product.image" class="img-placeholder-text">商品图片</div>
        </div>

        <div v-if="product.discount" class="discount-tag">{{ product.discount }}% OFF</div>
      </div>

      <!-- 商品信息 -->
      <div class="product-info">
        <h3 class="product-name">{{ product.name }}</h3>

        <div class="price-section">
          <div class="current-price">¥{{ product.price }}</div>
          <div v-if="product.originalPrice" class="original-price">
            ¥{{ product.originalPrice }}
          </div>
        </div>

        <div class="sales">已售 {{ product.sales }} 件</div>
      </div>
    </router-link>

    <!-- 操作按钮 -->
    <div class="action-bar">
      <button class="cart-btn">
        <svg-icon type="cart" />
        加入购物车
      </button>
    </div>
  </div>
</template>

<script setup lang="ts">
import { defineProps } from 'vue'
import type { Product } from '@/types/index'

// 定义属性
defineProps<{
  product: Product
}>()
</script>

<style scoped lang="scss">
.product-card {
  background: #fff;
  border-radius: 8px;
  overflow: hidden;
  transition: all 0.3s;
  position: relative;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
  height: 100%;
  display: flex;
  flex-direction: column;

  &:hover {
    transform: translateY(-5px);
    box-shadow: 0 8px 20px rgba(0, 0, 0, 0.1);

    .card-link::after {
      opacity: 1;
    }

    .cart-btn {
      transform: translateY(0);
      opacity: 1;
    }
  }

  .card-link {
    position: relative;
    text-decoration: none;
    color: inherit;
    flex: 1;

    &::after {
      content: '';
      position: absolute;
      top: 0;
      left: 0;
      right: 0;
      bottom: 0;
      background: rgba(0, 0, 0, 0.03);
      z-index: 1;
      opacity: 0;
      transition: opacity 0.3s;
    }
  }

  .product-image {
    position: relative;
    padding-top: 100%; // 正方形比例
    overflow: hidden;

    .image-placeholder {
      position: absolute;
      top: 0;
      left: 0;
      right: 0;
      bottom: 0;
      display: flex;
      align-items: center;
      justify-content: center;

      .img-placeholder-text {
        color: #aaa;
        font-size: 14px;
      }
    }

    .discount-tag {
      position: absolute;
      top: 10px;
      right: 10px;
      background: #f56c6c;
      color: white;
      padding: 2px 8px;
      font-size: 12px;
      border-radius: 4px;
      font-weight: bold;
    }
  }

  .product-info {
    padding: 15px;

    .product-name {
      font-size: 15px;
      font-weight: normal;
      margin: 0 0 10px;
      height: 42px;
      overflow: hidden;
      display: -webkit-box;
      -webkit-line-clamp: 2;
      -webkit-box-orient: vertical;
    }

    .price-section {
      display: flex;
      align-items: center;
      margin-bottom: 8px;

      .current-price {
        font-size: 20px;
        color: #f56c6c;
        font-weight: bold;
      }

      .original-price {
        margin-left: 8px;
        font-size: 14px;
        color: #999;
        text-decoration: line-through;
      }
    }

    .sales {
      font-size: 13px;
      color: #888;
    }
  }

  .action-bar {
    padding: 0 15px 15px;

    .cart-btn {
      width: 100%;
      background: linear-gradient(to right, #ff4e50, #f9d423);
      color: white;
      border: none;
      padding: 8px 0;
      border-radius: 4px;
      cursor: pointer;
      display: flex;
      align-items: center;
      justify-content: center;
      gap: 6px;
      font-size: 14px;
      transition: all 0.3s;
      transform: translateY(10px);
      opacity: 0;

      svg {
        width: 18px;
        height: 18px;
      }

      &:hover {
        transform: translateY(-2px);
        opacity: 0.95;
      }
    }
  }
}
</style>
